package com.ruan.utils;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruan.mapper.DepartmentMapper;
import com.ruan.pojo.Department;
import com.ruan.pojo.Employee;
import com.ruan.pojo.MonthCount;
import com.ruan.pojo.Position;
import com.ruan.service.DepartmentService;
import com.ruan.service.PositionService;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.UUID;


//@Component//utils既不属于service层也不属于controller层
@Repository
public class PoiUtils {

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private PositionService positionService;


    public static void exportMonthCount(List<MonthCount> monthCountList) throws Exception {
        XSSFWorkbook excel = new XSSFWorkbook(); // Create a new workbook
        XSSFSheet sheet = excel.createSheet("sheet1"); // Create a new sheet

        String[] headers = {
                "姓名", "部门", "职位", "手机号",
                "请假天数",  "出差次数", "加班时长",
                "旷工天数", "迟到次数", "早退次数", "实际出勤天数"};

        XSSFRow row1 = sheet.createRow(0); // Create the first row for headers
        for (int i = 0; i < headers.length; i++) {
            row1.createCell(i).setCellValue(headers[i]); // Create cell for each header
        }

        for (int i = 0; i < monthCountList.size(); i++) {
            MonthCount monthCount = monthCountList.get(i);
            XSSFRow row = sheet.createRow(i + 1);

            String[] monthCountInfo = {
//            monthCount.getName(), monthCount.getDepartment(),
//            monthCount.getPosition(),monthCount.getPhone(),
            String.valueOf(monthCount.getLeaveDay()), String.valueOf(monthCount.getTripCount()),
            String.valueOf(monthCount.getOverTimeHours()), String.valueOf(monthCount.getAwayWorkDay()),
            String.valueOf(monthCount.getLateCount()), String.valueOf(monthCount.getEarlyCount()),
            String.valueOf(monthCount.getOnDutyCount())};

            for (int j = 0; j < monthCountInfo.length; j++) {
                row.createCell(j).setCellValue(monthCountInfo[j]);
            }
        }

        String filePath = "D:/JSP/Ihrm/ihrm_system/src/main/resources/templates/month_count.xlsx";
        String fileName = "month_count.xlsx";
        OutputStream outputStream = new FileOutputStream(new File(filePath));
        excel.write(outputStream);
        outputStream.flush();
        excel.close();
        QiNiuUtils.uploadToQiNiu(filePath, fileName);
    }

//    public static void exportMonthCount(List<MonthCount> monthCountList) throws Exception {
//        XSSFWorkbook excel=new XSSFWorkbook(); //工作簿
//        //创建一个工作表对象
//        XSSFSheet sheet = excel.createSheet("sheet1");
//        //在工作表中创建行对象
//        XSSFRow row1 = sheet.createRow(0); //创建第一行表头
//        row1.createCell(0).setCellValue("姓名");//创建第一行第一个单元格
//        row1.createCell(1).setCellValue("部门");//创建第一行第二个单元格
//        row1.createCell(2).setCellValue("职位");//创建第一行第三个单元格
//        row1.createCell(3).setCellValue("手机号");//创建第一行第四个单元格
//        row1.createCell(4).setCellValue("请假天数");//创建第一行第五个单元格
//        row1.createCell(5).setCellValue("出差次数");//创建第一行第六个单元格
//        row1.createCell(6).setCellValue("加班时长");//创建第一行第七个单元格
//        row1.createCell(7).setCellValue("旷工天数");//创建第一行第八个单元格
//        row1.createCell(8).setCellValue("迟到次数");//创建第一行第九个单元格
//        row1.createCell(9).setCellValue("早退次数");//创建第一行第十个单元格
//        row1.createCell(10).setCellValue("实际出勤天数");//创建第十一行第四个单元格
//        for (int i = 0; i < monthCountList.size(); i++) {
//            MonthCount monthCount = monthCountList.get(i);
//            XSSFRow row2 = sheet.createRow(i+1);
//            row2.createCell(0).setCellValue(monthCount.getName());//创建第二行第一个单元格
//            row2.createCell(1).setCellValue(monthCount.getDepartment());//创建第二行第二个单元格
//            row2.createCell(2).setCellValue(monthCount.getPosition());//创建第二行第三个单元格
//            row2.createCell(3).setCellValue(monthCount.getPhone());//创建第二行第三个单元格
//            row2.createCell(4).setCellValue(monthCount.getLeaveDay());//创建第二行第三个单元格
//            row2.createCell(5).setCellValue(monthCount.getTripCount());//创建第二行第三个单元格
//            row2.createCell(6).setCellValue(monthCount.getOverTimeHours());//创建第二行第三个单元格
//            row2.createCell(7).setCellValue(monthCount.getAwayWorkDay());//创建第二行第三个单元格
//            row2.createCell(8).setCellValue(monthCount.getLateCount());//创建第二行第三个单元格
//            row2.createCell(9).setCellValue(monthCount.getEarlyCount());//创建第二行第三个单元格
//            row2.createCell(10).setCellValue(monthCount.getOnDutyCount());//创建第二行第三个单元格
//        }
//
//        //创建一个输出流，通过输出流将内存中的Excel文件写到磁盘
//        String fileName = "month_count.xlsx";
//        OutputStream outputStream=new FileOutputStream(new File(filePath));
//        excel.write(outputStream);
//        outputStream.flush();
//        excel.close();
//        QiNiuUtils.uploadToQiNiu(filePath,fileName);
//    }

    public void exportEmployeeInfo(List<Employee> employeeList) throws Exception {
        System.out.println("positionService是空的吗："+positionService);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        XSSFWorkbook excel=new XSSFWorkbook(); //工作簿，允许您在Java应用程序中创建、读取和修改Excel文件
        //创建一个工作表对象
        XSSFSheet sheet = excel.createSheet("sheet1");
        //在工作表中创建行对象
////        XSSFRow row1 = sheet.createRow(0); //创建第一行表头
////        row1.createCell(0).setCellValue("姓名");//创建第一行第一个单元格
////        row1.createCell(1).setCellValue("性别");//创建第一行第二个单元格
////        row1.createCell(2).setCellValue("手机号");//创建第一行第三个单元格
////        row1.createCell(3).setCellValue("邮箱");//创建第一行第四个单元格
////        row1.createCell(4).setCellValue("出生日期");//创建第一行第五个单元格
////        row1.createCell(5).setCellValue("家庭住址");//创建第一行第六个单元格
////        row1.createCell(6).setCellValue("现住址");//创建第一行第七个单元格
////        row1.createCell(7).setCellValue("学历");//创建第一行第八个单元格
////        row1.createCell(8).setCellValue("毕业院校");//创建第一行第九个单元格
////        row1.createCell(9).setCellValue("所在部门");//创建第一行第十个单元格
////        row1.createCell(10).setCellValue("职位");//创建第十一行第四个单元格
////        row1.createCell(11).setCellValue("入职时间");//创建第十一行第四个单元格
////        row1.createCell(12).setCellValue("离职时间");//创建第十一行第四个单元格
////        row1.createCell(13).setCellValue("状态");//创建第十一行第四个单元格

        String[] headers = {"姓名", "性别", "手机号", "邮箱", "出生日期", "省",
                "市", "区", "学历", "毕业院校", "所在部门", "职位", "入职时间", "离职时间", "状态"};

        XSSFRow row1 = sheet.createRow(0); // Create the first row for headers

        for (int i = 0; i < headers.length; i++) {
            row1.createCell(i).setCellValue(headers[i]); // Create cell for each header
        }

        for (int i = 0; i < employeeList.size(); i++) {
            Employee employee = employeeList.get(i);
            Date birth = employee.getBirth();
            Date worktime = employee.getWorktime();
            Date outtime = employee.getOuttime();
            XSSFRow row2 = sheet.createRow(i+1);
//
//
////            row2.createCell(0).setCellValue(employee.getName());//创建第二行第一个单元格
////            row2.createCell(1).setCellValue(employee.getGender());//创建第二行第二个单元格
////            row2.createCell(2).setCellValue(employee.getPhone());//创建第二行第三个单元格
////            row2.createCell(3).setCellValue(employee.getEmail());//创建第二行第三个单元格
////            row2.createCell(4).setCellValue(sdf.format(birth));//创建第二行第三个单元格
////            row2.createCell(5).setCellValue(employee.getHome());//创建第二行第三个单元格
////            row2.createCell(6).setCellValue(employee.getAddress());//创建第二行第三个单元格
////            row2.createCell(7).setCellValue(employee.getEducation());//创建第二行第三个单元格
////            row2.createCell(8).setCellValue(employee.getSchool());//创建第二行第三个单元格
////            row2.createCell(9).setCellValue(employee.getDepartment());//创建第二行第三个单元格
////            row2.createCell(10).setCellValue(employee.getPosition());//创建第二行第三个单元格
////            row2.createCell(11).setCellValue(sdf.format(worktime));//创建第二行第三个单元格
            if(employee != null){//departmentId
                Long departmentId = employee.getDepartmentId();
                Long positionId = employee.getPositionId();
            System.out.println("departmentId是:" + departmentId);
            System.out.println("positionId是:" + positionId);
            Department department = new Department();
            Position position = new Position();
//
                if(positionId != null && employee.getPositionId() != null) {
                    System.out.println("positionService是空的："+positionService);
                    position = positionService.getById(positionId);
                }

                if(employee != null && employee.getDepartmentId() != null) {
                    System.out.println("departmentService是空的："+departmentService);
                    System.out.println("departmentMapper是空的："+departmentMapper);
                    department = departmentService.getById(employee.getDepartmentId());//报空指针。。。。。。啊啊啊啊
                    System.out.println("department是：" + department);
                }
//


            String[] employeeInfo = {
                    employee.getName(), employee.getGender(),
                    employee.getPhone(), employee.getEmail(),
                    sdf.format(birth), employee.getProvinceName(),
                    employee.getCityName(), employee.getDistrictName(),
                    employee.getEducation(),employee.getSchool(),
                    department.getName(),position.getPname(),
                    sdf.format(worktime),
                    outtime != null ? sdf.format(outtime) : "",
                    "", employee.getStatus()};

            for (int j = 0; j < employeeInfo.length; j++) {
                row2.createCell(j).setCellValue(employeeInfo[j]);
            }
        }
            if (outtime!=null){
                row2.createCell(12).setCellValue(sdf.format(outtime));//创建第二行第三个单元格
            }
            row2.createCell(12).setCellValue("");//创建第二行第三个单元格
            row2.createCell(13).setCellValue(employee.getStatus());//创建第二行第三个单元格
        }

        //创建一个输出流，通过输出流将内存中的Excel文件写到磁盘
        String fileName = UUID.randomUUID().toString()+".xlsx";
        String filePath = "D:/DLSS/Excel文件/"+fileName;
//        String filePath = "D://Employee_Info.xlsx";

//        String fileName = "Employee_Info.xlsx";
        OutputStream outputStream=new FileOutputStream(new File(filePath));
        excel.write(outputStream);
        outputStream.flush();
        excel.close();
        QiNiuUtils.uploadToQiNiu(filePath,fileName);
    }
}
